﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Pointandclick;
using Pointandclick.Objects;

namespace Pointandclick.ScreenManagement
{
    class SpeedTestScreen : BaseScreen
    {

        Texture2D white;
        List<MovingClickable> clickList;
        TextMenuButton more, less;
        float speed=1;
        SpriteFont font;

        public SpeedTestScreen(ScreenManager manager)
        {
            Manager = manager;
            LoadContent();
            clickList = new List<MovingClickable>();
            for (int i = 0; i < 10; i++)
                clickList.Add(new MovingClickable(white, new Point(30 + 2 * i * white.Bounds.Width, 50)));
            more = new TextMenuButton("more", font);
            more.Rectangle = new Rectangle(50, 300, (int)font.MeasureString(more.Label).X, (int)font.MeasureString(more.Label).Y);
            less = new TextMenuButton("less", font);
            less.Rectangle = new Rectangle(200, 300, (int)font.MeasureString(more.Label).X, (int)font.MeasureString(more.Label).Y);
        }

        protected override void LoadContent()
        {
            white = Manager.Content.Load<Texture2D>("whiteRectangle");
            Background = Manager.Content.Load<Texture2D>("background");
            font = Manager.Content.Load<SpriteFont>("testFont");
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < clickList.Count; i++)
            {
                if (clickList[i].Update(speed, gameTime))
                    clickList.RemoveAt(i);
                else if (clickList[i].Rectangle.X + clickList[i].Rectangle.Width < 0)
                    clickList.RemoveAt(i);
            }
            if (clickList.Count < 10)
            {
                Rectangle temp = clickList.Last<MovingClickable>().Rectangle;
                clickList.Add(new MovingClickable(white, new Point(temp.X+2*temp.Width,temp.Y)));
            }
            if (more.Update()== more.Label) speed+=0.1f;
            if (less.Update() == less.Label) speed = Math.Max(speed-0.1f, 0);
        }

        public override void Draw(SpriteBatch spriteBatch, Color color)
        {
            spriteBatch.Draw(Background, new Vector2(0, 0), color);
            foreach (MovingClickable clickable in clickList)
                clickable.Draw(spriteBatch, color);
            spriteBatch.DrawString(font, more.Label, new Vector2(more.Rectangle.X, more.Rectangle.Y), color);
            spriteBatch.DrawString(font, less.Label, new Vector2(less.Rectangle.X, less.Rectangle.Y), color);
            spriteBatch.DrawString(font, "velocidade: " + speed, new Vector2(50, 400), color);
        }
    }
}
